package com.bruce.thread;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by bruce on 19/1/7 -- 16:03
 */
public class Test implements Runnable {
    private static int index = 0;
    private static Test myThread = new Test();
    private Lock lock = new ReentrantLock();

    public static void main(String[] args) throws InterruptedException {
        Thread runnable1 = new Thread(myThread);
        Thread runnable2 = new Thread(myThread);
        runnable1.start();
        runnable2.start();
        runnable1.join();
        runnable2.join();

        System.out.println("result>>>" + index);
    }

    @Override
    public void run() {
        lock.lock();
        for (int i = 0; i < 100; i++) {
            index++;
            System.out.println(Thread.currentThread().getName() + ">>>" + index);
        }
        lock.unlock();
    }
}
